package spellcheck;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * http://dl.dropboxusercontent.com/u/342881/spellcheck.htm
 * Your code should correct the following three classes of spelling mistakes:

*	Case (upper/lower) errors: "inSIDE" => "inside"
*	Repeated letters: "jjoobbb" => "job"
*	Incorrect vowels: "weke" => "wake"
 * 
 * Any combination of the above types of error 
 * in a single word should be corrected 
 * (e.g. "CUNsperrICY" => "conspiracy" or "ffoaoaoaoaoaoaaoaoaoaoaoadd" => "food").
 * @author Ray
 *
 */
public class spellCheck {

	HashMap<String, String> short_lower;
	HashMap<String, String> lower_org;

	public spellCheck() throws IOException,FileNotFoundException {

		this.short_lower = new HashMap<>();
		this.lower_org = new HashMap<>();
		BufferedReader br = new BufferedReader(new FileReader("src/Simple3/words.txt"));
		String line,temp;
		while ((line = br.readLine()) != null) {
			temp = transform(line);
			short_lower.put(temp, line.toLowerCase());
			lower_org.put(line.toLowerCase(), line);
		}
		br.close();
	}

	public void checker(String input){
		if(lower_org.containsKey(input.toLowerCase())){
			System.out.println(lower_org.get(input.toLowerCase()));
			return;
		}
		input = transform(input);
		if(!short_lower.containsKey(input)){
			System.out.println("NO SUGGESTION");
			return;
		}
		else{
			String print = short_lower.get(input);
			System.out.println(lower_org.get(print));
			return;
		}
	}
	
	private String transform(String word) {
		String ret = "";
		ArrayList<Character> temp = new ArrayList<>();
		String newword = word.toLowerCase();
		char[] ch = newword.toCharArray();
		for(int i=0;i<newword.length();++i){
			if(isVowel(ch[i])) 
				ch[i] = '#';
		}
		newword = new String(ch);
		temp.add(newword.charAt(0));
		int flag = 0;
		for (int i = 1; i < word.length(); ++i) {
			if (newword.charAt(i) != temp.get(flag)) {
				temp.add(newword.charAt(i));
				++flag;
			}
		}
		for (int i = 0; i < temp.size(); ++i)
			ret += temp.get(i);
		return ret;
	}

	private boolean isVowel(char c) {
		if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
			return true;
		return false;
	}

	public static void main(String[] args) throws FileNotFoundException, IOException {
		// TODO Auto-generated method stub

		
		spellCheck test = new spellCheck();
		while(true){
			System.out.print("> ");
			try {
				Scanner in = new Scanner(System.in);
				String input = in.nextLine();
				test.checker(input);
			} catch (Exception e) {
				System.err.println("Error Input");
				break;
			}
			
		}
	}

}
